import datetime

class Seq_pair(object):

    def __init__(self,si,ti,tt):
        self.seq_id=si
        self.time=ti
        self.tot_time=tt

    def print_info(self):
        print self.seq_id,'#',self.tot_time

class Trajectorie(object):

    def __init__(self,ii,ti,vi,st,tt):
        self.intersection_id=ii
        self.tollgate_id=ti
        self.vehicle_id=vi
        self.starting_time=st
        self.travel_seq=[]
        self.travel_time=tt

    def add_travel_seq(self,ts):
        if isinstance(ts,Seq_pair):
            self.travel_seq.append(ts)
        else:
            print 'not Seq_pair class'

    def print_info(self):
        print ('intersection_id=%s tollgate_id=%d vehicle_id=%d travel_time=%f'
               %(self.intersection_id,self.tollgate_id,self.vehicle_id,self.travel_time))
        print 'starting_time:',self.starting_time
        print 'travel_seq:'
        for i in self.travel_seq:
            i.print_info()

class Trajectories(object):

    def __init__(self):
        self.trajectories=[]

    def add_trajectorie(self,t):
        if isinstance(t,Trajectorie):
            self.trajectories.append(t)
        else:
            print 'not Trajectorie class'

    def print_info(self):
        for i in self.trajectories:
            i.print_info()

    def available_dates(self):
        res=[]
        for i in self.trajectories:
            if not i.starting_time.date() in res:
                res.append(i.starting_time.date())
        return res

    def averageTime(self, dates, linkid, seg1, seg2):
        res = {}
        for i in dates:
            res[i] = []
        for t in self.trajectories:
            tmp = t.starting_time.hour * 3 + t.starting_time.minute / 20
            if t.starting_time.date() in dates and seg1 <= tmp and tmp < seg2:
                for l in t.travel_seq:
                    if l.seq_id == linkid:
                        if(l.tot_time > 50):
                            t.print_info()
                        res[t.starting_time.date()].append(l.tot_time)
        ans = []
        for k in sorted(res.keys()):
            if(len(res[k]) > 0):
                ans.append(sum(res[k]) * 1.0 / len(res[k]))
            else:
                ans.append(0)
        return ans

def Trajectories_test():
    a=Trajectorie('A',123,4567,datetime.datetime(2015, 12, 16, 15, 6, 37, 420000),66.6)
    a.add_travel_seq(Seq_pair(11,datetime.datetime(2015, 12, 16, 15, 6, 37, 420000)),10.5)
    a.add_travel_seq(Seq_pair(11,datetime.datetime(2015, 12, 16, 15, 6, 37, 420000)),12.5)
    b=Trajectories()
    b.add_trajectorie(a)
    b.print_info()
